/*
 @description: 后端资讯

 @author: 李建翰

 @update: 2021年9月4日12:58:35
*/
'use strict';
const Controller = require('egg').Controller;
class RealtimeInfoCpntroller extends Controller{
    
    async index() { //搜索
        const {ctx,app} = this;
        const {keyname,page=1,number=4} = ctx.request.query
        const offset=(page-1)*number
        let data,count
        if(keyname && page){
            data = await app.mysql.query(`select id,title,content,time_d,release_z from fuxiao_news where title like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_news where title like '%${keyname}%'`)
        }else if(keyname){
            data = await app.mysql.query(`select id,title,content,time_d,release_z from fuxiao_news where title ='%${keyname}%' or title  like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_news where title like '%${keyname}%' or title like '%${keyname}%'`)
        }else{
            data=await app.mysql.query(`select id,title,content,time_d,release_z from fuxiao_news limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from fuxiao_news`)
        }
        ctx.body = {
            code:0,
            msg:"查找成功",
            data,
            count:count[0].count
        }
    }

    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('fuxiao_news',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }
            
            // 添加
            async create(){     //post
                const { ctx,app} = this;
                const {title,content,release_z}=ctx.request.body
                if(title && content && release_z){
                    const data=await app.mysql.insert('fuxiao_news',{title,content,release_z,time_d:Date.now()
                });
                    ctx.body = {
                        code: 0,
                        msg: "新增成功",
                    };
                } else {
                    ctx.body = {
                        code: 4006,
                        msg: "添加失败",
                    };
                }
            }
            //改
            async update() {//put
                const {ctx,app} =this;
                const {title,content,release_z} = ctx.request.body
                const {id} = ctx.params;
                const row={
                    title:title,
                    content:content,
                    release_z:release_z,
                    time_d:Date.now()
                };
                const options={
                    where:{
                        id:+id
                    }
                };
                const result=await app.mysql.update('fuxiao_news',row,options);
                    if(result){
                        ctx.body={
                            code:0,
                            msg:"编辑成功",
                        };
                    }else{
                        ctx.body={
                            code:4008,
                            msg:"编辑失败",
                        };
                    } 
            }

            //删除
            async destroy() { //DELETE
                const {ctx,app} = this;
                const {id} = ctx.params;
                const result =await app.mysql.delete(`fuxiao_news`,{id:+id})
                if(result){
                    ctx.body = {
                        code: 0,
                        msg: "删除成功",
                    };
                }else{
                    ctx.body = {
                        code: 4007,
                        msg: "删除失败",
                    }; 
                }
                }
}
module.exports = RealtimeInfoCpntroller